home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2001 September / PC-WELT 9-2001.ISO / software / hw / brennen / flask_src.exe / Audio / AC3 / AC3Dec / bitstream.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-06  |  2.8 KB  |  130 lines

  1. /* 
  2.  *  bitstream.c
  3.  *
  4.  *    Copyright (C) Aaron Holtzman - Dec 1999
  5.  *
  6.  *  This file is part of ac3dec, a free AC-3 audio decoder
  7.  *    
  8.  *  ac3dec is free software; you can redistribute it and/or modify
  9.  *  it under the terms of the GNU General Public License as published by
  10.  *  the Free Software Foundation; either version 2, or (at your option)
  11.  *  any later version.
  12.  *   
  13.  *  ac3dec is distributed in the hope that it will be useful,
  14.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  *  GNU General Public License for more details.
  17.  *   
  18.  *  You should have received a copy of the GNU General Public License
  19.  *  along with GNU Make; see the file COPYING.  If not, write to
  20.  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
  21.  *
  22.  */
  23.  
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26.  
  27. #include "ac3.h"
  28. #include "ac3_internal.h"
  29. #include "bitstream.h"
  30.  
  31. #define BUFFER_SIZE 4096
  32.  
  33. static uint_8 buffer[BUFFER_SIZE];
  34.  
  35. static uint_8 *buffer_start, *buffer_end;
  36. static uint_8 *chunk_start, *chunk_end;
  37.  
  38. uint_32 bits_left;
  39. uint_32 current_word;
  40.  
  41. void (*bitstream_fill_buffer)(uint_8**,uint_8**);
  42.  
  43. uint_8 bitstream_get_byte(void)
  44. {
  45.     if(chunk_start == chunk_end)
  46.         bitstream_fill_buffer(&chunk_start,&chunk_end);
  47.  
  48.     return (*chunk_start++);
  49. }
  50.  
  51. uint_8 *bitstream_get_buffer_start(void)
  52. {
  53.     return buffer_start;
  54. }
  55.  
  56. void
  57. bitstream_buffer_frame(uint_32 frame_size)
  58. {
  59.   uint_32 bytes_read;
  60.   uint_32 num_bytes;
  61.  
  62.   bytes_read = 0;
  63.  
  64.   do
  65.   {
  66.     if(chunk_start > chunk_end)
  67.             printf("argh!\n");
  68.     if(chunk_start == chunk_end)
  69.       bitstream_fill_buffer(&chunk_start,&chunk_end);
  70.  
  71.     num_bytes = chunk_end - chunk_start;
  72.  
  73.     if(bytes_read + num_bytes > frame_size)
  74.       num_bytes = frame_size - bytes_read;
  75.  
  76.     memcpy(&buffer[bytes_read], chunk_start, num_bytes);
  77.  
  78.     bytes_read += num_bytes;
  79.     chunk_start += num_bytes;
  80.   }
  81.   while (bytes_read != frame_size);
  82.  
  83.   buffer_start = buffer;
  84.   buffer_end   = buffer + frame_size;
  85.  
  86.     bits_left = 0;
  87. }
  88.  
  89.  
  90. static inline void
  91. bitstream_fill_current()
  92. {
  93.     current_word = *((uint_32*)buffer_start)++;
  94.     current_word = swab32(current_word);
  95. }
  96.  
  97. //
  98. // The fast paths for _get is in the
  99. // bitstream.h header file so it can be inlined.
  100. //
  101. // The "bottom half" of this routine is suffixed _bh
  102. //
  103. // -ah
  104. //
  105.  
  106. uint_32
  107. bitstream_get_bh(uint_32 num_bits)
  108. {
  109.     uint_32 result;
  110.  
  111.     num_bits -= bits_left;
  112.     result = (current_word << (32 - bits_left)) >> (32 - bits_left);
  113.  
  114.     bitstream_fill_current();
  115.  
  116.     if(num_bits != 0)
  117.         result = (result << num_bits) | (current_word >> (32 - num_bits));
  118.     
  119.     bits_left = 32 - num_bits;
  120.  
  121.     return result;
  122. }
  123.  
  124. void
  125. bitstream_init(void(*fill_function)(uint_8**,uint_8**))
  126. {
  127.     // Setup the buffer fill callback 
  128.     bitstream_fill_buffer = fill_function;
  129. }
  130.